package com.lx.test.web;//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//




import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.fastjson.parser.Feature;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.lx.test.exception.ParamException;
import com.lx.test.log.exception.ResponseType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.core.annotation.AnnotatedElementUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

@Component
@Order(3)
public class HsafRequestMappingHandlerAdapter extends RequestMappingHandlerAdapter {
    private Logger log = LoggerFactory.getLogger(HsafRequestMappingHandlerAdapter.class);
    static int SUCCESS = 0;
    static int FAIL = -1;
    static int PARAM_VALID_EXCEPTION = -2;
    static String MSG_SUCCESS = "成功";
    static String MSG_FAIL = "未知的异常";
    private String LOG_CONTEXT_KEY = "_logContext";
    @Autowired
    private ApplicationContext applicationContext;

    static Map<Type, Object> basicTypeMap = new HashMap();

    public HsafRequestMappingHandlerAdapter() {
    }

    protected boolean supportsInternal(HandlerMethod handlerMethod) {
        this.log.debug("[hsaf]调用HsafRequestMappingHandlerAdapter.supportsInternal()方法，handlerMethod=" + handlerMethod);
        return AnnotatedElementUtils.hasAnnotation(handlerMethod.getMethod(), HsafRestPath.class);
    }

    protected ModelAndView handleInternal(HttpServletRequest request, HttpServletResponse response, HandlerMethod handlerMethod) throws Exception {
         return    super.handleInternal(request,response,handlerMethod);
        //        this.log.debug("[hsaf]调用HsafRequestMappingHandlerAdapter.handleInternal()方法，handlerMethod=" + handlerMethod);
//        ModelAndView mv = new ModelAndView(new MappingJackson2JsonView());
//        Method m = handlerMethod.getMethod();
//        Object hostBean = this.applicationContext.getBean(m.getDeclaringClass());
//        String methodType = request.getMethod();
//        if (methodType != null && !methodType.equals("") && (methodType.equalsIgnoreCase("GET") || methodType.equalsIgnoreCase("POST"))) {
//            Object returnObj = null;
//
//            try {
//                Parameter[] ps = m.getParameters();
//                if (ps != null && ps.length != 0) {
//                    Object[] arglist;
//                    String jsonStr;
//                    String clz;
//                    if (ps.length == 1) {
//                        if (methodType.equalsIgnoreCase("GET")) {
//                            if (!this.isBasicType(ps[0].getType())) {
//                                mv.addObject("code", FAIL);
//                                mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                                mv.addObject("message", "非基本类型的参数无法通过GET请求");
//                                return mv;
//                            }
//
//                            arglist = new Object[1];
//                            String queryString = request.getQueryString();
//                            if (queryString != null && !queryString.equals("") && !queryString.equals("null")) {
//                                if (queryString.length() > 0) {
//                                    try {
//                                        queryString = URLDecoder.decode(queryString, "utf-8");
//                                    } catch (Exception var24) {
//                                        this.log.error("请求串转码异常");
//                                    }
//                                }
//
//                                this.log.debug("queryString=" + queryString);
//                                String paramString = queryString.substring(queryString.indexOf("=") + 1);
//                                arglist[0] = this.getObject(ps[0].getType(), paramString);
//                                returnObj = m.invoke(hostBean, arglist);
//                                mv.addObject("code", SUCCESS);
//                                mv.addObject("type", ResponseType.TYPE_SUCCESS.getType());
//                                mv.addObject("message", MSG_SUCCESS);
//                                mv.addObject("data", returnObj);
//                                return mv;
//                            }
//                            mv.addObject("code", PARAM_VALID_EXCEPTION);
//                            mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                            mv.addObject("message", "请求参数个数不匹配");
//                            return mv;
//                        }
//
//                        jsonStr = this.getJsonStrFromRequest(request);
//                        this.log.debug("jsonStr=" + jsonStr);
//                        Class typeClass = ps[0].getType();
//                        this.log.debug("typeClass=" + typeClass + ";isBasicType=" + this.isBasicType(typeClass) + ";isArray=" + typeClass.isArray() + ";isList=" + typeClass.isAssignableFrom(List.class) + ";isMap=" + typeClass.isAssignableFrom(Map.class));
//                        if (this.isBasicType(typeClass)) {
//                            Object paramObj = this.getObject(typeClass, jsonStr);
//                            returnObj = m.invoke(hostBean, paramObj);
//                        } else if (typeClass.isArray()) {
//                            JSONArray jsonArray = JSONArray.parseArray(jsonStr);
//                            arglist = new Object[1];
//                            Object array;ke(hostBean, jmap);
//                                }
//                            } else {
//                                arglist = new Object[1];
//                                Object paramObj = null;
//
//                                try {
//                                    if (this.isBasicType(typeClass.getComponentType())) {
//                                        array = Array.newInstance(typeClass.getComponentType(), jsonArray.size());
//
//                                        for(int i = 0; i < jsonArray.size(); ++i) {
//                                            Array.set(array, i, jsonArray.get(i));
//                                        }
//
//                                        arglist[0] = array;
//                                        returnObj = m.invoke(hostBean, arglist);
//                                    } else {
//                                        array = Array.newInstance(typeClass.getComponentType(), jsonArray.size());
//
//                                        for(int i = 0; i < jsonArray.size(); ++i) {
//                                            JSONObject jsonObj = jsonArray.getJSONObject(i);
//                                            Object paramObj = JSON.toJavaObject(jsonObj, typeClass.getComponentType());
//                                            Array.set(array, i, paramObj);
//                                        }
//
//                                        arglist[0] = array;
//                                        returnObj = m.invoke(hostBean, arglist);
//                                    }
//                                } else {
//                                    Type type;
//                                    ParameterizedType pt;
//                                    Type t;
//                                    if (typeClass.isAssignableFrom(List.class)) {
//                                        type = ps[0].getParameterizedType();
//                                        if (type instanceof ParameterizedType) {
//                                            pt = (ParameterizedType)type;
//                                            t = pt.getActualTypeArguments()[0];
//                                            Class clz1 = Class.forName(t.getTypeName());
//                                            JSONArray jsonArray = JSONArray.parseArray(jsonStr);
//                                            List list = new ArrayList();
//
//                                            for(int i = 0; i < jsonArray.size(); ++i) {
//                                                Object paramObj;
//                                                if (this.isBasicType(clz1)) {
//                                                    paramObj = jsonArray.getObject(i, t);
//                                                } else {
//                                                    JSONObject jsonObj = jsonArray.getJSONObject(i);
//                                                    paramObj = jsonObj.toJavaObject(t);
//                                                }
//
//                                                list.add(paramObj);
//                                            }
//
//                                            returnObj = m.invoke(hostBean, list);
//                                        }
//                                    } else if (typeClass.isAssignableFrom(Map.class)) {
//                                        type = ps[0].getParameterizedType();
//                                        if (type instanceof ParameterizedType) {
//                                            pt = (ParameterizedType)type;
//                                            t = pt.getActualTypeArguments()[0];
//                                            Type t1 = pt.getActualTypeArguments()[1];
//                                            Class clz0 = Class.forName(t.getTypeName());
//                                            Class clz1 = Class.forName(t1.getTypeName());
//                                            Map map = (Map)JSONObject.parseObject(jsonStr, typeClass);
//                                            Map jmap = new HashMap();
//
//                                            Object kobj;
//                                            Object rvobj;
//                                            for(Iterator it = map.keySet().iterator(); it.hasNext(); jmap.put(clz0.getConstructor(String.class).newInstance(kobj), rvobj)) {
//                                                kobj = it.next();
//                                                if (this.isBasicType(clz1)) {
//                                                    rvobj = map.get(kobj);
//                                                } else {
//                                                    JSONObject vobj = (JSONObject)map.get(kobj);
//                                                    rvobj = vobj.toJavaObject(t1);
//                                                }
//                                            }
//
//                                            returnObj = m.invo
//                                    JSONObject jsonobj = JSONObject.parseObject(jsonStr);
//                                    if (jsonobj != null && jsonobj.containsKey("_class")) {
//                                        clz = jsonobj.getString("_class");
//                                        if (clz != null && clz.length() > 1) {
//                                            type = ((TypeReference)Class.forName(clz).newInstance()).getType();
//                                            paramObj = JSONObject.parseObject(jsonStr, type, new Feature[0]);
//                                        }
//                                    }
//                                } catch (Exception var26) {
//                                    this.log.error("_class参数非法");
//                                    var26.printStackTrace();
//                                }
//
//                                if (paramObj == null) {
//                                    if (m.getGenericParameterTypes()[0].getTypeName().length() == 1) {
//                                        paramObj = JSONObject.parseObject(jsonStr, typeClass);
//                                    } else {
//                                        paramObj = JSONObject.parseObject(jsonStr, m.getGenericParameterTypes()[0], new Feature[0]);
//                                    }
//                                }
//
//                                arglist[0] = paramObj;
//                                returnObj = m.invoke(hostBean, arglist);
//                            }
//                        }
//                    } else {
//                        int i;
//                        Iterator var14;
//                        if (methodType.equalsIgnoreCase("GET")) {
//                            jsonStr = request.getQueryString();
//                            if (jsonStr != null && jsonStr.length() > 0) {
//                                try {
//                                    jsonStr = URLDecoder.decode(jsonStr, "utf-8");
//                                } catch (Exception var25) {
//                                    this.log.error("请求串转码异常");
//                                }
//                            }
//
//                            this.log.debug("queryString=" + jsonStr);
//                            if (jsonStr == null || jsonStr.trim().equals("")) {
//                                mv.addObject("code", FAIL);
//                                mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                                mv.addObject("message", "请求参数个数不匹配");
//                                return mv;
//                            }
//
//                            List<String> list = this.convertToList(jsonStr.trim());
//                            if (list.size() != ps.length) {
//                                mv.addObject("code", FAIL);
//                                mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                                mv.addObject("message", "请求参数个数不匹配");
//                                return mv;
//                            }
//
//                            i = 0;
//                            arglist = new Object[ps.length];
//
//                            for(var14 = list.iterator(); var14.hasNext(); ++i) {
//                                clz = (String)var14.next();
//                                arglist[i] = this.getObject(ps[i].getType(), clz);
//                            }
//
//                            returnObj = m.invoke(hostBean, arglist);
//                        } else {
//                            jsonStr = this.getJsonStrFromRequest(request);
//                            JSONObject jsonObj = JSONObject.parseObject(jsonStr);
//                            if (jsonObj.size() != ps.length) {
//                                mv.addObject("code", FAIL);
//                                mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                                mv.addObject("message", "请求参数个数不匹配");
//                                return mv;
//                            }
//
//                            i = 0;
//                            arglist = new Object[ps.length];
//
//                            for(var14 = jsonObj.values().iterator(); var14.hasNext(); ++i) {
//                                Object o = var14.next();
//                                arglist[i] = o;
//                            }
//
//                            returnObj = m.invoke(hostBean, arglist);
//                        }
//                    }
//                } else {
//                    returnObj = m.invoke(hostBean);
//                }
//            } catch (Exception var27) {
//                if (var27 instanceof InvocationTargetException) {
//                    Throwable target = ((InvocationTargetException)var27).getTargetException();
//                    if (target != null && target instanceof UndeclaredThrowableException) {
//                        Throwable undeclaredThrowable = ((UndeclaredThrowableException)target).getUndeclaredThrowable();
//                        if (undeclaredThrowable != null && undeclaredThrowable instanceof ParamException) {
//                            mv.addObject("code", PARAM_VALID_EXCEPTION);
//                            mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                            mv.addObject("message", undeclaredThrowable.getMessage());
//                            return mv;
//                        }
//                    }
//                } else {
//                    if (var27 instanceof ParamException) {
//                        mv.addObject("code", PARAM_VALID_EXCEPTION);
//                        mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                        mv.addObject("message", var27.getMessage());
//                        return mv;
//                    }
//
//                    if (var27.getCause() != null && var27.getCause() instanceof ParamException) {
//                        mv.addObject("code", PARAM_VALID_EXCEPTION);
//                        mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//                        mv.addObject("message", var27.getCause().getMessage());
//                        return mv;
//                    }
//                }
//
//                throw var27;
//            }
//            mv.addObject("code", SUCCESS);
//            mv.addObject("type", ResponseType.TYPE_SUCCESS.getType());
//            mv.addObject("message", MSG_SUCCESS);
//            mv.addObject("data", returnObj);
//            return mv;
//        } else {
//            mv.addObject("code", FAIL);
//            mv.addObject("type", ResponseType.TYPE_ERROR.getType());
//            mv.addObject("message", "请求参数类型异常，当前版本仅支持GET、POST。");
//            return mv;
//        }
    }
    private boolean isBasicType(Class typeClass) {
        if (typeClass.isPrimitive()) {
            return true;
        } else {
            return basicTypeMap.containsKey(typeClass);
        }
    }

    private Object getObject(Class clazz, String paramString) throws Exception {
        if (clazz.isPrimitive()) {
            if (clazz.getSimpleName().equalsIgnoreCase("boolean")) {
                return Boolean.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("char")) {
                return paramString.charAt(0);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("byte")) {
                return Byte.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("short")) {
                return Short.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("int")) {
                return Integer.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("long")) {
                return Long.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("float")) {
                return Float.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("double")) {
                return Double.valueOf(paramString);
            }

            if (clazz.getSimpleName().equalsIgnoreCase("void")) {
                return Void.TYPE;
            }
        }

        if (clazz.getSimpleName().equalsIgnoreCase("Character")) {
            return paramString.charAt(0);
        } else if (clazz.getSimpleName().equalsIgnoreCase("void")) {
            return Void.TYPE;
        } else {
            Constructor c1 = clazz.getConstructor(String.class);
            return c1.newInstance(paramString);
        }
    }

    private List<String> convertToList(String queryString) {
        List<String> list = new ArrayList();
        queryString = queryString.replaceAll("&amp;", "&");
        String[] qs = queryString.split("&");
        String[] var4 = qs;
        int var5 = qs.length;

        for(int var6 = 0; var6 < var5; ++var6) {
            String s = var4[var6];
            list.add(s.substring(s.indexOf("=") + 1));
        }

        return list;
    }

    private String getJsonStrFromRequest(HttpServletRequest request) {
        try {
            InputStream in = request.getInputStream();
            ByteArrayOutputStream result = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];

            int length;
            while((length = in.read(buffer)) != -1) {
                result.write(buffer, 0, length);
            }

            in.close();
            String jsonStr = result.toString("UTF-8");
            result.close();
            return jsonStr;
        } catch (Exception var7) {
            this.log.error("从request请求输入流中获取数据异常，异常信息如下:");
            var7.printStackTrace();
            return null;
        }
    }



    static {
        basicTypeMap.put(String.class, (Object)null);
        basicTypeMap.put(Number.class, (Object)null);
        basicTypeMap.put(Boolean.class, (Object)null);
        basicTypeMap.put(Character.class, (Object)null);
        basicTypeMap.put(Byte.class, (Object)null);
        basicTypeMap.put(Short.class, (Object)null);
        basicTypeMap.put(Integer.class, (Object)null);
        basicTypeMap.put(Long.class, (Object)null);
        basicTypeMap.put(Float.class, (Object)null);
        basicTypeMap.put(Double.class, (Object)null);
    }
}
